package com.auto.survey.activity.monitoring

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.Adapter
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.auto.survey.MainActivity.Companion.KEY_MENU_IMAGE
import com.auto.survey.MainActivity.Companion.KEY_MENU_INTEN
import com.auto.survey.MainActivity.Companion.KEY_MENU_TITLE
import com.auto.survey.R
import com.auto.survey.activity.bluetooth.BluetoolsActivity
import com.auto.survey.activity.monitoring.IntentKey.KEY_INTENT_OBJECT_ID
import com.auto.survey.activity.monitoring.IntentKey.KEY_INTENT_PROJET_ID
import com.auto.survey.activity.monitoring.point.ManagerPointActivity
import com.auto.survey.activity.monitoring.station.MonitoringCollectionActivity
import com.auto.survey.base.BaseActivity
import com.auto.survey.log.AppLogger

class MonitoringActivity: BaseActivity() {

    private var projectId:Int = -1
    private var objectId:Int = -1

    override fun initView() {

        projectId = intent.getIntExtra(KEY_INTENT_PROJET_ID, -1)
        objectId = intent.getIntExtra(KEY_INTENT_OBJECT_ID, -1)

        mTopBar?.visibility = View.VISIBLE
        mTopBar?.setBackgroundResource(R.color.action_bar)

        mComment_title?.visibility = View.VISIBLE
        mComment_title?.setBackgroundResource(R.color.action_bar)
        mComment_title?.setTitle("自动化监测")


        val rv_monitoring = findViewById<RecyclerView>(R.id.rv_monitoring)
        rv_monitoring.layoutManager = LinearLayoutManager(this)
        val adapter = MonitoringAtyAdapter(this@MonitoringActivity, projectId, objectId)
        rv_monitoring.adapter = adapter
        adapter.setDatas(menuData())
    }

    fun menuData(): MutableList<Map<String, Any>> {
        val datas: MutableList<Map<String, Any>> = ArrayList()

        val titls = arrayOf("仪器连接", "测点管理", "监测初始采集")
        val imges = arrayOf(R.mipmap.login_bull_no_border, R.mipmap.login_bull_no_border, R.mipmap.login_bull_no_border)
        val targs = arrayOf(0x2111, 0x2112, 0x2113)

        for (i in titls.indices) {
            val menu1 = HashMap<String, Any>()
            menu1[KEY_MENU_TITLE] = titls[i]
            menu1[KEY_MENU_IMAGE] = imges[i]
            menu1[KEY_MENU_INTEN] = targs[i]

            datas.add(menu1)
        }


        return datas;
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_monitoring
    }

    private class MonitoringAtyAdapter(context: Context, projectId:Int, objectId:Int) : Adapter<MonitoringAtyViewHodler>(){
        private val mContext: Context = context
        private val mProjectId:Int = projectId
        private val mObjectId:Int = objectId

        private val mData: MutableList<Map<String, Any>> = ArrayList()
        fun setDatas(data:List<Map<String, Any>>) {
            mData.clear()
            mData.addAll(data)
            notifyDataSetChanged();
        }


        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MonitoringAtyViewHodler {
            val view = LayoutInflater.from(parent.context).inflate(R.layout.adapter_menu, parent, false);
            return MonitoringAtyViewHodler(view)
        }

        override fun getItemCount(): Int {
            if (mData.size > 0) {
                return mData.size;
            }

            return 0;
        }

        override fun onBindViewHolder(holder: MonitoringAtyViewHodler, position: Int) {
            holder.imag.setImageResource(mData[position][KEY_MENU_IMAGE] as Int)
            holder.text.text = mData[position][KEY_MENU_TITLE] as String
            holder.parent.setOnClickListener {
                try {
                    // 用户操作日志
                    AppLogger.getInstance(mContext).logOperation("MonitoringActivity", "adapter click")
                    val intent = Intent()
                    if ((mData[position][KEY_MENU_INTEN]) as Int == 0x2111){
                        intent.setClass(mContext, BluetoolsActivity::class.java)
                    } else if ((mData[position][KEY_MENU_INTEN]) as Int == 0x2112) {
                        intent.putExtra(KEY_INTENT_PROJET_ID, mProjectId)
                        intent.putExtra(KEY_INTENT_OBJECT_ID, mObjectId)
                        intent.setClass(mContext, ManagerPointActivity::class.java)
                    } else  if ((mData[position][KEY_MENU_INTEN]) as Int == 0x2113) {
                        intent.putExtra(KEY_INTENT_PROJET_ID, mProjectId)
                        intent.putExtra(KEY_INTENT_OBJECT_ID, mObjectId)
                        intent.setClass(mContext, MonitoringCollectionActivity::class.java)
                    }
                    mContext.startActivity(intent)

                } catch (e:Exception) {
                    e.printStackTrace()
                    AppLogger.getInstance(mContext).e("MonitoringActivity", "adapter click failed", e)
                }
            }
        }

    }

    private class MonitoringAtyViewHodler(itemview:View):ViewHolder(itemview) {
        val imag = itemView.findViewById<ImageView>(R.id.iv_grid_menu)
        val text = itemView.findViewById<TextView>(R.id.tv_grid_menu)
        val parent = itemView.findViewById<ConstraintLayout>(R.id.cl_grid_menu)
    }

}